<!--签约人群分类统计-->
<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-row>
              <el-col :span="6">
                <el-form-item label="年份" prop="year">
                  <el-date-picker
                    v-model="queryParams.year"
                    type="date"
                    placeholder="选择日期"
                    value-format="YYYY-MM-DD"
                    unlink-panels
                    @change="handleQuery"
                    style="width: 240px"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="季度">
                  <el-select v-model="queryParams.quarter" placeholder="请选择">
                    <el-option label="第一季度" value="shanghai"></el-option>
                    <el-option label="第一季度" value="beijing"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="机构名称">
                  <el-select v-model="queryParams.GgName" placeholder="请选择">
                    <el-option label="第一季度" value="shanghai"></el-option>
                    <el-option label="第一季度" value="beijing"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="团队">
                  <el-select v-model="queryParams.team" placeholder="请选择">
                    <el-option label="第一季度" value="shanghai"></el-option>
                    <el-option label="第一季度" value="beijing"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>

                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <el-table v-loading="loading" :data="demoList" @selection-change="handleSelectionChange">
        <el-table-column v-if="true" label="编码" align="center" prop="id" />
        <el-table-column label="机构" align="center" prop="institution" />
        <el-table-column label="常驻人口数" align="center" prop="inhabitant" />
        <el-table-column label="新增签约量" align="center" prop="institutionNum" />
        <el-table-column label="签约已过失效人数" align="center" prop="Family" />
        <el-table-column label="签约率" align="center" prop="doctorNum" />
        <el-table-column label="签约有效人数" align="center" prop="doctorNum" />
        <el-table-column label="续约数" align="center" prop="doctorNum" />
        <el-table-column label="续约率" align="center" prop="doctorNum" />
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Demo" lang="ts">
import { listDemo, getDemo, delDemo, addDemo, updateDemo } from '@/api/demo/demo';
import { hygiene, hygieneQuery, hygieneForm } from '@/api/demo/demo/hygiene';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const demoList = ref<hygiene[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const demoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: hygieneForm = {
  year: undefined,
  quarter: undefined,
  GgName: undefined,
  team: undefined,
};
const data = reactive<PageData<hygieneForm, hygieneQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    year: undefined,
    quarter: undefined,
    GgName: undefined,
    team: undefined,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询测试单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDemo(queryParams.value);
  demoList.value = res.rows;
  console.log('111',res.rows)
  total.value = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: hygiene[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length === 1;
  multiple.value = selection.length > 0;
};

/** 提交按钮 */
const submitForm = async () => {
  demoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateDemo(form.value);
      } else {
        await addDemo(form.value);
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
      buttonLoading.value = false;
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: hygiene) => {
  const _ids = row?.id || ids.value[0];
  await proxy?.$modal.confirm(`是否确认删除测试单编号为"${_ids}"的数据项？`).finally(() => (loading.value = false));
  await delDemo(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

onMounted(() => {
  getList();
});
</script>
